<?php
/**
 *  plugin.func.php 公共函数库
 *
 * @copyright			(C) 2005-2010 PHPCMS
 * @license				http://www.phpcms.cn/license/
 * @lastmodify			2010-6-1
 */
/**
 * [prw 将数组(任意维度)写入一个HTML文件],只在调试模试下执行
 * @author 李海涛 <QQ:596845798>
 * @return [type]           [返回一个字窜]
 */
function prw(){

    $args = func_get_args ();
    $debugArr = debug_backtrace ();

    $filename = 'cbg.html';
    // if( substr(strrchr($filename, '.'), 1) != 'html'){
    //     $filename .= '.html';
    // }
    $debugArr = debug_backtrace ();
    $laststr  = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    $laststr .= "<pre  style='color:red'>【调用文件】:". $debugArr [0] ['file']. '<br/>【调用行号】:'. $debugArr [0] ['line'] . '<br/>';
    $laststr .= '【调用时间】:'.date('Y-m-d H:i:s',time()).'<hr/>';
    foreach ( $args as $k => $v ) {
        $laststr .= '【变量序号】:' . ($k + 1) .  '<br/>【变量类型】:' . gettype ( $v ) . '<br/>';
        $laststr .= print_r ( $v ,true);
        $laststr .= '<hr>';
    }
    $laststr .= '</pre>';
    file_put_contents($filename, $laststr);
}



/**
 * [prw 将数组(任意维度)写入一个HTML文件],只在调试模试下执行,并退出
 * @author 李海涛 <QQ:596845798>
 */
function prew(){

    $args = func_get_args ();
    $debugArr = debug_backtrace ();

    $filename = 'cbg.html';
    // if( substr(strrchr($filename, '.'), 1) != 'html'){
    //     $filename .= '.html';
    // }
    $debugArr = debug_backtrace ();
    $laststr  = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    $laststr .= "<pre  style='color:red'>【调用文件】:". $debugArr [0] ['file']. '<br/>【调用行号】:'. $debugArr [0] ['line'] . '<br/>';
    $laststr .= '【调用时间】:'.date('Y-m-d H:i:s',time()).'<hr/>';
    foreach ( $args as $k => $v ) {
        $laststr .= '【变量序号】:' . ($k + 1) .  '<br/>【变量类型】:' . gettype ( $v ) . '<br/>';
        $laststr .= print_r ( $v ,true);
        $laststr .= '<hr>';
    }
    $laststr .= '</pre>';
    file_put_contents($filename, $laststr);
    exit(1);
}

/**
 * 测试函数 执行完退出 可以传入任意个参数,只在调试模试下执行
 * @author 李海涛 <QQ:596845798>
 */
function pre() {
    

    // $argsNum = func_num_args(); //获取参数个数
    $args = func_get_args ();
    $debugArr = debug_backtrace ();
    echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    echo "<pre  style='color:red'><hr><hr>【调用文件】:", $debugArr [0] ['file'], '<br/>【调用行号】:', $debugArr [0] ['line'], '<br/>';
    echo '【调用时间】:',date('Y-m-d H:i:s',time()),'<hr>';
    foreach ( $args as $k => $v ) {
        echo '【变量序号】:', $k + 1, '<br/>【变量类型】:', gettype ( $v ), '<br/>';
        print_r ( $v );
        echo '<hr>';
    }
    echo '<hr></pre>';
    exit ();
}

/**
 * 测试函数 执行完不退出 可以传入任意个参数,只在调试模试下执行
 * @author 李海涛 <QQ:596845798>
 */
function pr() {

    // $argsNum = func_num_args(); //获取参数个数
    $args = func_get_args ();
    $debugArr = debug_backtrace ();
    echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    echo "<pre  style='color:red'><hr><hr>【调用文件】:", $debugArr [0] ['file'], '<br/>【调用行号】:', $debugArr [0] ['line'], '<br/>';
    echo '【调用时间】:',date('Y-m-d H:i:s',time()),'<hr>';
    foreach ( $args as $k => $v ) {
        echo '【变量序号】:', $k + 1, '<br/>【变量类型】:', gettype ( $v ), '<br/>';
        print_r ( $v );
        echo '<hr>';
    }
    echo '<hr></pre>';
}




function p($array) {
	dump ( $array, 1, '<pre style=font-size:14px;color:#00ae19;>', 0 );
}
function dump($var, $echo=true, $label=null, $strict=true) {
    $label = ($label === null) ? '' : rtrim($label) . ' ';
    if (!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        } else {
            $output = $label . print_r($var, true);
        }
    } else {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if (!extension_loaded('xdebug')) {
            $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        }
    }
    if ($echo) {
        echo($output);
        return null;
    }else
        return $output;
}
function is_mobile() { 
    $user_agent = $_SERVER['HTTP_USER_AGENT']; 
    $mobile_agents = array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi", 
    "android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio", 
    "au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu", 
    "cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ", 
    "fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi", 
    "htc","huawei","hutchison","inno","ipad","ipaq","iphone","ipod","jbrowser","kddi", 
    "kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo", 
    "mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-", 
    "moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia", 
    "nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-", 
    "playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo", 
    "samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank", 
    "sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit", 
    "tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin", 
    "vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce", 
    "wireless","xda","xde","zte"); 
    $is_mobile = false; 
    foreach ($mobile_agents as $device) { 
        if (stristr($user_agent, $device)) { 
            $is_mobile = true; 
            break; 
        } 
    } 
    return $is_mobile; 
} 
function pluginkey_check($key) {
	return preg_match("/^[a-z]+[a-z0-9_]*$/i", $key);
}

/**
 * 插件语言包
 * Enter description here ...
 * @param unknown_type $language
 * @param unknown_type $pars
 * @param unknown_type $plugin
 */
function pluginlang($language = 'no_language',$pars = array(), $plugin = '') {
	static $PLUGIN_LANG = array();
	static $PLUGIN_MODULES = array();
	if(!$PLUGIN_LANG && defined('PLUGIN_ID')) {
		if(file_exists(PC_PATH.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.PLUGIN_ID.DIRECTORY_SEPARATOR.'languages'.DIRECTORY_SEPARATOR.PLUGIN_ID.'.lang.php')) require PC_PATH.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.PLUGIN_ID.DIRECTORY_SEPARATOR.'languages'.DIRECTORY_SEPARATOR.PLUGIN_ID.'.lang.php';
	}	
	if(!empty($plugin)) {
		require PC_PATH.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'languages'.DIRECTORY_SEPARATOR.$plugin.'.lang.php';
	}
	if(!array_key_exists($language,$PLUGIN_LANG)) {
		return L('no_language');
	} else {
		$language = $PLUGIN_LANG[$language];
		if($pars) {
			foreach($pars AS $_k=>$_v) {
				$language = str_replace('{'.$_k.'}',$_v,$language);
			}
		}
		return $language;
	}	
}


function plugin_stat($appid = '') {
	if(pc_base::load_config('system','plugin_debug')) return 2;
	$agent = $_SERVER['HTTP_USER_AGENT'];
	if(strpos($agent, 'Maxthon')!== FALSE) {
		$pars['brower'] = 'Maxthon';
	} elseif(strpos($agent, 'SE 2.X MetaSr 1.0')!== FALSE) {
		$pars['brower'] = 'Sougou';
	} elseif(strpos($agent, 'TencentTraveler')!== FALSE) {
		$pars['brower'] = 'TencentTraveler';
	} elseif(strpos($agent, 'MSIE 9.0')!== FALSE) {
		$pars['brower'] = 'MSIE 9.0';
	} elseif(strpos($agent, 'MSIE 8.0')!== FALSE) {
		$pars['brower'] = 'MSIE 8.0';
	} elseif(strpos($agent, 'MSIE 7.0')!== FALSE) {
		$pars['brower'] = 'MSIE 7.0';
	} elseif(strpos($agent, 'MSIE 6.0')!== FALSE) {
		$pars['brower'] = 'MSIE 6.0';
	} elseif(strpos($agent, 'Firefox/4')!== FALSE) {
		$pars['brower'] = 'Firefox 4';
	} elseif(strpos($agent, 'Firefox/3')!== FALSE) {
		$pars['brower'] = 'Firefox 3';
	} elseif(strpos($agent, 'Firefox/2')!== FALSE) {
		$pars['brower'] = 'Firefox 2';
	} elseif(strpos($agent, 'Chrome')!== FALSE) {
		$pars['brower'] = 'Chrome';
	} elseif(strpos($agent, 'Safari')!== FALSE) {
		$pars['brower'] = 'Safari';
	} elseif(strpos($agent, 'Opera')!== FALSE) {
		$pars['brower'] = 'Opera';
	}elseif(substr($agent, 0, 7) == 'Mozilla') {
		$pars['brower'] = 'Mozilla';
	} else {
		$pars['brower'] = 'Other';
	}
	
	if(strpos($agent, 'Win')!== FALSE) {
		$pars['os'] = 'Windows';
	} elseif(strpos($agent, 'Mac')!== FALSE) {
		$pars['os'] = 'Mac';
	} elseif(strpos($agent, 'Linux')!== FALSE) {
		$pars['os'] = 'Linux';
	} elseif(strpos($agent, 'FreeBSD')!== FALSE) {
		$pars['os'] = 'FreeBSD';
	} elseif(strpos($agent, 'SunOS')!== FALSE) {
		$pars['os'] = 'SunOS';
	} elseif(strpos($agent, 'OS/2')!== FALSE) {
		$pars['os'] = 'OS/2';
	} elseif(strpos($agent, 'AIX')!== FALSE) {
		$pars['os'] = 'AIX';
	} elseif(preg_match("/(Bot|Crawl|Spider)/i", $agent)) {
		$pars['os'] = 'Spiders';
	} else {
		$pars['os'] = 'Other';
	}
	$pars['ip'] = ip2long(ip());
	$pars['domain'] = urlencode(SITE_PROTOCOL.SITE_URL);
	$data = http_build_query($pars);
	$url = 'http://open.phpcms.cn/api.php?op=appstatus&'.$data.'&appid='.$appid;
	$headers = get_headers($url,1);	
	$status = $headers['pc_appstatus'];
	return $status;
}

/**
*插件安装量统计
*/
function plugin_install_stat($appid){
	if(pc_base::load_config('system','plugin_debug')) return false;
	$appid = intval($appid);
	if($appid == 0 ) return false;
	$url = 'http://open.phpcms.cn/api.php?op=appstatus&isinstall=1';
	$headers = get_headers($url.'&appid='.$appid,1);	 	
}

/**
* 插件合法性验证
*/
function app_validity_check($appid) {
	if(pc_base::load_config('system','plugin_debug')) return 2;
	$appid = intval($appid);
	$url = $header = '';
	$url = 'http://open.phpcms.cn/api.php?op=appstatus';
	$headers = get_headers($url.'&appid='.$appid,1);
	$status = $headers['pc_appstatus'];
	return $status;
}
	
function plugin_url($data = '',$type = '') {
	$args = '';
	if($data == '') {
		$args = $_GET['args'] ? '-'.$_GET['args'] : '';		
	} elseif(is_array($data)) {
		$args = '-'.args_encode($data);
	}
	return  $type == '' ? 'plugin.php?id='.PLUGIN_ID.'-'.PLUGIN_FILE.'-'.PLUGIN_ACTION.'&args='.substr($args,1) : 'plugin-'.PLUGIN_ID.'-'.PLUGIN_FILE.'-'.PLUGIN_ACTION.$args.'.html';
}


/**
*加密需通过get方式在url中传递的参数
*/
function args_encode($data) {
	if(is_array($data)) {
		$string = http_build_query($data);
		return base64_encode($string);
	} else {
		return false;
	}
}
/**
*获取url中get方式传递的参数
*/
function getargs() {
	$string = base64_decode($_GET['args']);
	parse_str($string,$g);
	return $g;
}
?>